package com.hskn.hss.app.violation.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.controller.BaseController;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.utils.AlarmUtils;
import com.hskn.hss.utils.AttributeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * APP违规数据
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Api(tags = {"-violation"}, description = "违规数据")
@Slf4j
@RestController
@RequestMapping("/hss/app/violation")
public class APPViolationController {

    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;

    /**
     * 首页违规数据
     * @param
     * @return
     */
    @ApiOperation(value = "首页违规数据", notes = "首页违规数据")
    @RequestMapping(value = "selectViolation", method = RequestMethod.POST)
    public AjaxResult<Object> selectAlarm() {
        try {
            String day = DateUtil.formatDate(new Date());
            String startTime = AttributeUtils.timeCompletion(day, 's');
            String endTime = AttributeUtils.timeCompletion(day, 'e');

            QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
            carViolationQueryWrapper.lambda().ge(CarViolation::getTime,startTime);
            carViolationQueryWrapper.lambda().le(CarViolation::getTime,endTime);
            List<CarViolation> carViolations = carViolationMapper.selectList(carViolationQueryWrapper);

            QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
            dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate,startTime);
            dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate,endTime);
            List<DlbjViolation> dlbjViolations = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);

            //车的违规
            Map<String, Long> carViolationMap = carViolations.stream().collect(Collectors.groupingBy(CarViolation::getRuleTypeName, Collectors.counting()));
            Map<String,Object> carMap = new HashMap();
            carMap.put("speedlimit",0);
            carMap.put("vehicleStays",0);
            carMap.put("emptyVehicle",0);
            carViolationMap.keySet().forEach(t->{
                if ("速度限制".equals(t)){
                    carMap.put("speedlimit",carViolationMap.get(t));
                } else if ("车辆停留".equals(t)){
                    carMap.put("vehicleStays",carViolationMap.get(t));
                } else if ("车辆空驶".equals(t)){
                    carMap.put("emptyVehicle",carViolationMap.get(t));
                }
            });
            if (Objects.nonNull(carViolations)){
                carMap.put("num",carViolations.size());
            } else {
                carMap.put("num",0);
            }
            carMap.put("type","car");

            //人的违规
            Map<String, Long> dlbjViolationMap = dlbjViolations.stream().collect(Collectors.groupingBy(DlbjViolation::getViolationTypeName, Collectors.counting()));
            Map<String,Object> empMap = new HashMap();
            empMap.put("remain",0);
            empMap.put("leaving",0);
            empMap.put("gatherTheCrowd",0);
            dlbjViolationMap.keySet().forEach(t->{
                if ("停留".equals(t)){
                    empMap.put("remain",dlbjViolationMap.get(t));
                } else if ("离岗".equals(t)){
                    empMap.put("leaving",dlbjViolationMap.get(t));
                } else if ("聚众".equals(t)){
                    empMap.put("gatherTheCrowd",dlbjViolationMap.get(t));
                }
            });
            if (Objects.nonNull(dlbjViolations)){
                empMap.put("num",dlbjViolations.size());
            } else {
                empMap.put("num",0);
            }
            empMap.put("type","emp");

            List list = new ArrayList();
            list.add(carMap);
            list.add(empMap);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

}

